package com.password.safe.component;

import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import com.password.safe.constants.Constants;

public abstract class AbstractMainFrame extends JFrame {
	private static final long serialVersionUID = 8784841135147621409L;
	private static Logger logger = Logger.getLogger(AbstractMainFrame.class);

	// edit password components
	// edit menu
	private JMenu editMenu;
	private JMenuBar menuBar;
	
	private JMenu manageMenu;
	
	// logout menu
	private JMenu logoutMenu;
	
	// exit system menu
	private JMenu exitMenu;
	
	// edit area
	private JTextArea editArea;
	
	private JTextField hiddenField;
	
	protected AbstractMainFrame() {
		this.setTitle("Password Safe");
		// set frmae's look and location
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 200));// 设定窗口出现位置
        this.setSize(600, 600);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		// add Edit menubar
		menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		editMenu = new JMenu("Edit");
		menuBar.add(editMenu);
		
		
		/*// add undo menu
		JMenuItem undoItem = createUndoMenuItem();
		editMenu.add(undoItem);
		
		// add redo menu
		JMenuItem redoItem = createRedoMenuItem();
		editMenu.add(redoItem);*/
		
		// add save menu
		JMenuItem saveItem = createSaveMenuItem();
		editMenu.add(saveItem);
		
		/*// add select menu
		JMenuItem selectItem = createSelectMenuItem();
		editMenu.add(selectItem);
		
		// add clear menu
		JMenuItem clearItem = createClearMenuItem();
		editMenu.add(clearItem);*/
		
		manageMenu = new JMenu(Constants.BUTTON_MANAGE);
		JMenuItem updatePasswordItem = createUpdatePasswordMenuItem();
		manageMenu.add(updatePasswordItem);
		menuBar.add(manageMenu);
		
		// add logout menu
		logoutMenu = createLogoutMenu();
		menuBar.add(logoutMenu);
		
		// exit menu
		exitMenu = createExitMenu();
		menuBar.add(exitMenu);
		
		// add text erea
		editArea = createTextEdit();
		this.add(this.editArea);
	}

	private JTextArea createTextEdit() {
		JTextArea tempArea = new JTextArea();
		tempArea.addKeyListener(new KeyListener() {
			boolean enterCtr = false;
			
			public void keyTyped(KeyEvent e) {
			}
			
			public void keyReleased(KeyEvent e) {
			}
			
			public void keyPressed(KeyEvent e) {
				if (enterCtr && e.getKeyCode() == Constants.KEY_CODE_CTRL) {
					handleSaveRequest();
				} else if (e.getKeyCode() == Constants.KEY_CODE_S) {
					enterCtr = true;
				} else {
					enterCtr = false;
				}
			}
		});
		return tempArea;
	}

	public JTextArea getEditArea() {
		return this.editArea;
	}

	public JMenu getSaveMenu() {
		return editMenu;
	}
	
	public JTextField getHiddenField() {
		return hiddenField;
	}

	/**
	 *  handle undo request
	 * @since  2011-11-20
	 * @author chengpeng.zhang
	 */
	protected abstract void handleUndoRequest();
	
	/**
	 *  handle redo request
	 * @since  2011-11-20
	 * @author chengpeng.zhang
	 */
	protected abstract void handleRedoRequest();
	
	/**
	 *  handle save request
	 * @since  2011-11-20
	 * @author chengpeng.zhang
	 */
	protected abstract void handleSaveRequest();
	
	/**
	 *  handle clear request
	 * @since  2011-11-20
	 * @author chengpeng.zhang
	 */
	protected abstract void handleClearRequest();
	
	/**
	 * handle password request
	 * @since  2012-2-25
	 * @author chengpeng.zhang
	 */
	protected abstract void handleUpdatePasswordRequest();
	
	/**
	 * handle user click logout button
	 * Description of this Method
	 * @since  2012-2-11
	 * @author chengpeng.zhang
	 */
	protected abstract void handleLogoutRequest();
	
	/**
	 * handle user click Exit button
	 * Description of this Method
	 * @since  2012-2-11
	 * @author chengpeng.zhang
	 */
	protected abstract void handleExitRequest();
	
	/**
	 *  handle select request
	 * @since  2011-11-20
	 * @author chengpeng.zhang
	 */
	protected void handleSelectRequest() {
		throw new UnsupportedOperationException("handleClickSelectEvent must override by subclass.");
	}
	
	/*// init the undo menu item and add click event.
	private JMenuItem createUndoMenuItem() {
		JMenuItem undoItem = new JMenuItem(Constants.BUTTON_UNDO);
		undoItem.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
			}
			
			public void mousePressed(MouseEvent e) {
				handleClickUndoEvent();
				
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
			}
			
			public void mouseClicked(MouseEvent e) {
				handleClickUndoEvent();
			}
		});
		return undoItem;
	}
	
	// init the redo menu item and add click event.
	private JMenuItem createRedoMenuItem() {
		JMenuItem redoItem = new JMenuItem(Constants.BUTTON_REDO);
		redoItem.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
			}
			
			public void mousePressed(MouseEvent e) {
				handleClickRedoEvent();
				
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
			}
			
			public void mouseClicked(MouseEvent e) {
				handleClickRedoEvent();
			}
		});
		return redoItem;
	}*/
	
	// init the save menu item and add click event.
	private JMenuItem createSaveMenuItem() {
		JMenuItem saveMenuItem = new JMenuItem(Constants.BUTTON_SAVE);
		saveMenuItem.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
			}
			
			public void mousePressed(MouseEvent e) {
				handleSaveRequest();
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
			}
			
			public void mouseClicked(MouseEvent e) {
				handleSaveRequest();
			}
		});
		return saveMenuItem;
	}
	
	private JMenuItem createUpdatePasswordMenuItem() {
		JMenuItem tempMenuItem = new JMenuItem(Constants.BUTTON_UPDATE_PASSWORD);
		tempMenuItem.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				handleUpdatePasswordRequest();
			}
			
			public void mousePressed(MouseEvent e) {
			}
			
			public void mouseExited(MouseEvent e) {
			}
			
			public void mouseEntered(MouseEvent e) {
			}
			
			public void mouseClicked(MouseEvent e) {
			}
		});
		return tempMenuItem;
	}
	
	/*// init the clear menu item and add click event.
	private JMenuItem createClearMenuItem() {
		JMenuItem clearItem = new JMenuItem(Constants.BUTTON_CLEAR);
		clearItem.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
			}
			
			public void mousePressed(MouseEvent e) {
				handleClickClearEvent();
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
			}
			
			public void mouseClicked(MouseEvent e) {
				handleClickClearEvent();
			}
		});
		return clearItem;
	}*/
	
	private JMenu createUpdatePasswordMenu() {
		JMenu tempMenu = new JMenu(Constants.BUTTON_UPDATE_PASSWORD);
		return tempMenu;
	}
	
	private JMenu createLogoutMenu() {
		JMenu tempMenu = new JMenu(Constants.BUTTON_LOGOUT);
		tempMenu.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
			}
			
			public void mousePressed(MouseEvent e) {
				handleLogoutRequest();
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
			}
			
			public void mouseClicked(MouseEvent e) {
				handleLogoutRequest();
			}
		});
		return tempMenu;
	}

	/*// init the select menu item and add click event.
	private JMenuItem createSelectMenuItem() {
		JMenuItem selectItem = new JMenuItem(Constants.BUTTON_SELECT);
		selectItem.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
			}
			
			public void mousePressed(MouseEvent e) {
				handleClickSelectEvent();
				
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
			}
			
			public void mouseClicked(MouseEvent e) {
				handleClickSelectEvent();
			}
		});
		return selectItem;
	}*/
	
	private JMenu createExitMenu() {
		JMenu tempMenu = new JMenu(Constants.BUTTON_EXIT);
		tempMenu.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// do nothing
				
			}
			
			public void mousePressed(MouseEvent e) {
				handleExitRequest();
				
			}
			
			public void mouseExited(MouseEvent e) {
				// do nothing
				
			}
			
			public void mouseEntered(MouseEvent e) {
				// do nothing
				
			}
			
			public void mouseClicked(MouseEvent e) {
				handleExitRequest();
				
			}
		});
		return tempMenu;
	}
}

